use tracing::debug;

use bc_base::{self, BlockHeight, BlockId, TransactionValue, transaction::Transaction};
use bc_lib::mining::MiningCommand;

use crate::BcManager;

impl BcManager {
    pub async fn send_cmd_to_ming_task(&self, cmd: MiningCommand) {
        debug!("Send cmd to mining task: {:?}", &cmd);
        self.mining_manager
            .lock()
            .await
            .get_mining_command_sender()
            .send(cmd)
            .await
            .expect("Failed to send command to mining manager");
    }

    // TODO: send tx to mine task
    pub async fn send_tx_to_mine_task(&self, tx: Transaction) {
        let mining_mng = self.mining_manager.lock().await;
        mining_mng
            .get_transaction_sender()
            .send(tx)
            .await
            .expect("Failed to send transaction to mining manager");
    }

    pub async fn get_next_work_required_for_mining(&self) -> u32 {
        let consensus = self.consensus.lock().await;
        let prev_block_id = consensus.read_latest_block_id_in_best_chain().await;
        consensus.get_next_work_required(&prev_block_id).await
    }

    pub async fn read_latest_block_id_in_best_chain(&self) -> BlockId {
        let consensus = self.consensus.lock().await;
        consensus.read_latest_block_id_in_best_chain().await
    }

    pub async fn read_best_height(&self) -> BlockHeight {
        let consensus = self.consensus.lock().await;
        consensus.read_best_height().await
    }

    pub async fn get_adjusted_timestamp(&self) -> u64 {
        let consensus = self.consensus.lock().await;
        consensus.get_adjusted_timestamp().await
    }
    pub async fn get_subsidy_for_mining(&self) -> TransactionValue {
        self.consensus.lock().await.get_subsidy_for_mining().await
    }
}
